import 'dart:ui';

extension StringExt on String? {
  String toImage({String suffix = "png"}) {
    return "images/$this.$suffix";
  }

  bool isNull() {
    return this == null || this == "";
  }

  Color toColor({double? alpha}) {
    String pre = "";
    if (this?.length == 6) {
      pre = "0xFF";
    }
    if (this?.length == 8) {
      pre = "0x";
    }
    Color color = Color(int.parse(pre + (this == null ? "FFFFFF" : this!)));
    if (alpha != null) {
      color = color.withAlpha((alpha * 255).toInt());
    }
    return color;
  }

  String trimZero() {
    if (this == null) return "";
    String tmp = this!;
    if (!tmp.contains('.')) {
      return tmp;
    }
    while (tmp.endsWith('0')) {
      tmp = tmp.substring(0, tmp.length - 1);
    }
    if (tmp.endsWith('.')) {
      tmp = tmp.substring(0, tmp.length - 1);
    }
    return tmp;
  }

  num toNum() {
    return num.tryParse(this ?? "") ?? 0;
  }
}

class StringUtils {
  StringUtils._();

  static bool isNull(String? str) {
    return str == null || str == "";
  }
}
